package org.springblade.modules.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liquibase.repackaged.org.apache.commons.lang3.StringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.RedisKeyConstant;
import org.springblade.common.enums.item.ItemOperateType;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.classes.entity.ClassesEntity;
import org.springblade.modules.classes.entity.ClassesPlanEntity;
import org.springblade.modules.classes.entity.ClassesUserEntity;
import org.springblade.modules.classes.entity.TrainingPlanEntity;
import org.springblade.modules.classes.excel.TrainingYearExcel;
import org.springblade.modules.classes.service.IClassesPlanService;
import org.springblade.modules.classes.service.IClassesService;
import org.springblade.modules.classes.service.IClassesUserService;
import org.springblade.modules.classes.service.ITrainingPlanService;
import org.springblade.modules.classes.vo.ClassesVO;
import org.springblade.modules.classes.wrapper.ClassesPlanWrapper;
import org.springblade.modules.classes.wrapper.ClassesWrapper;
import org.springblade.modules.course.entity.CourseEntity;
import org.springblade.modules.course.entity.SubVideoReadEntity;
import org.springblade.modules.course.service.ICourseService;
import org.springblade.modules.course.service.ISubVideoReadService;
import org.springblade.modules.course.vo.CourseVO;
import org.springblade.modules.item.entity.ItemEntity;
import org.springblade.modules.item.entity.ItemStockLogEntity;
import org.springblade.modules.item.service.IItemService;
import org.springblade.modules.item.service.IStockLogService;
import org.springblade.modules.item.vo.ItemStockLogVO;
import org.springblade.modules.item.vo.ItemVO;
import org.springblade.modules.item.wrapper.StockLogWrapper;
import org.springblade.modules.library.entity.MaterialLibraryEntity;
import org.springblade.modules.library.entity.QuestionBankEntity;
import org.springblade.modules.library.service.IMaterialLibraryService;
import org.springblade.modules.library.service.IQuestionBankService;
import org.springblade.modules.log.entity.IntegralLogEntity;
import org.springblade.modules.log.entity.IntegralYearLogEntity;
import org.springblade.modules.log.service.IIntegralLogService;
import org.springblade.modules.log.service.IIntegralYearLogService;
import org.springblade.modules.material.entity.PaperAnswerEntity;
import org.springblade.modules.material.entity.PaperReleaseEntity;
import org.springblade.modules.material.entity.ReleaseExamUserEntity;
import org.springblade.modules.material.entity.TrainArchiveEntity;
import org.springblade.modules.material.service.IAnswerService;
import org.springblade.modules.material.service.IPaperReleaseService;
import org.springblade.modules.material.service.IReleaseExamUserService;
import org.springblade.modules.material.service.ITrainArchiveService;
import org.springblade.modules.personal.entity.SecrecyLevel;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.entity.UserTeacherEntity;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springblade.modules.personal.service.IUserTeacherService;
import org.springblade.modules.resource.enums.AttachType;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springblade.modules.teaching.entity.CourseChapterSubBankEntity;
import org.springblade.modules.teaching.entity.CourseChapterSubFileEntity;
import org.springblade.modules.teaching.service.ICourseChapterSubBankService;
import org.springblade.modules.teaching.service.ICourseChapterSubFileService;
import org.springblade.modules.teaching.vo.CourseChapterSubFileVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

import static org.springblade.modules.api.controller.JxcExternalController.*;

/**
 * 数据分析 控制器
 *
 * @author junxin
 * @since 2023-03-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-analyse")
@Api(value = "数据分析/数据大屏", tags = "数据分析/数据大屏接口")
@Slf4j
public class AnalyseController {

	private final IIntegralLogService integralLogService;
	private final IStockLogService stockLogService;
	private final IUserService userService;
	private final IPaperReleaseService paperReleaseService;
	private final IReleaseExamUserService releaseExamUserService;
	private final ICourseService courseService;
	private final IClassesUserService classesUserService;
	private final IAnswerService answerService;
	private final ICourseChapterSubFileService courseChapterSubFileService;
	private final IQuestionBankService questionBankService;
	private final IClassesService classesService;
	private final IUserTeacherService userTeacherService;
	private final BladeRedis bladeRedis;
	private final ITrainingPlanService trainingPlanService;
	private final IUserBaseService userBaseService;
	private final IItemService iItemService;
	private final IClassesPlanService classesPlanService;
	private final IIntegralYearLogService integralYearLogService;
	private final ITrainArchiveService trainArchiveService;
	private final IMaterialLibraryService materialLibraryService;
	private final ISubVideoReadService subVideoReadService;
	private final ICourseChapterSubBankService courseChapterSubBankService;

	/**
	 * 系统资源
	 */
	@GetMapping("/bigdata/system-resource")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "数据大屏-系统资源", notes = "")
	public R bigdataSystemResourceCount( ) {
		Map<String,Long> reuslt=new HashMap<>();
		//培训课程
		long courseCount = courseService.count();
		//培训视频
		long vedioCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
			.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_VEDIO.getCategory()));

		//培训课件
		long coursewareCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
			.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_COURSEWARE.getCategory()));

		//题库
		long questionBankCount = questionBankService.count();

		//培训教具 借出量
		long itemCount = stockLogService.count();

		//内训师
		long teacherCount=userTeacherService.count();
		reuslt.put("course",courseCount);//培训课程
		reuslt.put("vedio",vedioCount);//培训视频
		reuslt.put("courseware",coursewareCount);//培训课件
		reuslt.put("questionBank",questionBankCount);//题库
		reuslt.put("item",itemCount);//培训教具 借出量
		reuslt.put("teacher",teacherCount);//内训师
		return R.data(reuslt);
	}


	/**
	 * 系统资源-柱状图
	 * 当年的每月数据
	 */
	@GetMapping("/bigdata/system-resource-chart")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "数据大屏-系统资源-当年度的每月数据", notes = "课程-1，培训视频-2，培训课件-3，培训教具-4，题库-5，内训师-6")
	public R bigdataSystemResourceChart(Integer type) {
		List<Object> resultList=new ArrayList<>();
		String year= DateUtil.format(new Date(), "yyyy");
		Integer[] monthList = getMonth();

		for (Integer month : monthList) {
			LocalDateTime startTime=LocalDateTime.of(Integer.parseInt(year),month,1,0,0,0);
			LocalDateTime endTime = startTime.with(TemporalAdjusters.lastDayOfMonth());//当月最后一天
			long count=0;
			switch (type){
				case 1: //课程
					 count = courseService.count(Wrappers.<CourseEntity>query().lambda()
						.ge(CourseEntity::getCreateTime, startTime)
						.le(CourseEntity::getCreateTime, endTime));
					break;
				case 2: //培训视频
					 count = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
						 .eq(CourseChapterSubFileEntity::getAttachType,AttachType.COURSE_VEDIO.getCategory())
						 .ge(CourseChapterSubFileEntity::getCreateTime, startTime)
						 .le(CourseChapterSubFileEntity::getCreateTime, endTime));
					break;
				case 3: //培训课件
					 count = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
						 .eq(CourseChapterSubFileEntity::getAttachType,AttachType.COURSE_COURSEWARE.getCategory())
						 .ge(CourseChapterSubFileEntity::getCreateTime, startTime)
						 .le(CourseChapterSubFileEntity::getCreateTime, endTime));
					break;
				case 4: //培训教具
					 count = stockLogService.count(Wrappers.<ItemStockLogEntity>query().lambda()
						.ge(ItemStockLogEntity::getCreateTime, startTime)
						.le(ItemStockLogEntity::getCreateTime, endTime));
					break;
				case 5: //题库
					 count =questionBankService.count(Wrappers.<QuestionBankEntity>query().lambda()
						 .ge(QuestionBankEntity::getCreateTime, startTime)
						 .le(QuestionBankEntity::getCreateTime, endTime));
					break;
				case 6: //内训师
					 count =userTeacherService.count(Wrappers.<UserTeacherEntity>query().lambda()
						 .ge(UserTeacherEntity::getCreateTime, startTime)
						 .le(UserTeacherEntity::getCreateTime, endTime));
					break;
			}

			resultList.add(count);
		}
		return R.data(resultList);
	}


	/**
	 * 系统资源-明细列表
	 * 各种资源的列表
	 */
	@GetMapping("/bigdata/system-resource-list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "数据大屏-系统资源-明细列表", notes = "课程-1，培训视频-2，培训课件-3，培训教具-4，题库-5，内训师-6")
	public R bigdataSystemResourceList(Integer type) {
		List<Object> resultList=new ArrayList<>();
		String year= DateUtil.format(new Date(), "yyyy");
		Query query =new Query();
		query.setSize(500);//设置最大查询500条
			switch (type){
				case 1: //课程
					CourseVO course=new CourseVO();
					course.setSecrecyLevel(SecrecyLevel.SECRET.getCode());
					IPage<CourseVO> coursePages = courseService.selectCoursePage(Condition.getPage(query), course);
					return R.data(coursePages);
				case 2: //培训视频
					CourseChapterSubFileVO vedioSubFile=new CourseChapterSubFileVO();
					vedioSubFile.setAttachType(AttachType.COURSE_VEDIO.getCategory());
					IPage<CourseChapterSubFileVO> subVedioPages = courseChapterSubFileService.selectCourseChapterSubFilePage(Condition.getPage(query), vedioSubFile);
					return R.data(subVedioPages);
				case 3: //培训课件
					CourseChapterSubFileVO coursewareSubFile=new CourseChapterSubFileVO();
					coursewareSubFile.setAttachType(AttachType.COURSE_COURSEWARE.getCategory());
					IPage<CourseChapterSubFileVO> subCoursewarePages = courseChapterSubFileService.selectCourseChapterSubFilePage(Condition.getPage(query), coursewareSubFile);

					return R.data(subCoursewarePages);
				case 4: //培训教具 借阅记录
					LambdaQueryWrapper<ItemStockLogEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
					IPage<ItemStockLogEntity> pages = stockLogService.page(Condition.getPage(query), lambdaQueryWrapper);

					IPage<ItemStockLogVO> itemStockLogVOIPage = StockLogWrapper.build().pageVO(pages);
					itemStockLogVOIPage.getRecords().forEach(itemStockLogVO -> {
						UserBaseEntity userBaseEntity = userBaseService.selectByUserId(itemStockLogVO.getCreateUser());
						if (null == userBaseEntity) return;
						ItemEntity item = iItemService.getById(itemStockLogVO.getItemId());
						if (null != item) itemStockLogVO.setItemName(item.getItemName());
						itemStockLogVO.setUserName(userBaseEntity.getUserName());
						itemStockLogVO.setIdCard(userBaseEntity.getUserNumber());
					});

					return R.data(itemStockLogVOIPage);
				case 5: //题库
					LambdaQueryWrapper<QuestionBankEntity> questionBankQueryWrapper=new LambdaQueryWrapper<>();
					questionBankQueryWrapper.orderByDesc(QuestionBankEntity::getCreateTime);
					IPage<QuestionBankEntity> questionBnakPages = questionBankService.page(Condition.getPage(query),questionBankQueryWrapper );
					return R.data(questionBnakPages);
				case 6: //内训师
					List<UserTeacherEntity> userTeacherList = userTeacherService.list();
					if (CollectionUtil.isEmpty(userTeacherList)) return R.success("暂无数据");
					List<Long> teacherIds = userTeacherList.stream().map(UserTeacherEntity::getUserId).collect(Collectors.toList());

					LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
					userLambdaQueryWrapper.in(User::getId,teacherIds);
					IPage<User> userPages = userService.page(Condition.getPage(query), userLambdaQueryWrapper);
					IPage<UserVO> userVOIPage = UserWrapper.build().pageVO(userPages);
					userVOIPage.getRecords().forEach(userVo -> {
						UserBaseEntity userBaseEntity = userBaseService.selectByUserId(userVo.getId());
						if (null != userBaseEntity) {
							userVo.setIdCard(userBaseEntity.getUserNumber());
							userVo.setUserName(userBaseEntity.getUserName());
							userVo.setTrainingMajorCategory(userBaseEntity.getTrainingMajorCategory());
							userVo.setTrainingMajorDirection(userBaseEntity.getTrainingMajorDirection());
							userVo.setAppointmentGrade(userBaseEntity.getAppointmentGrade());
							userVo.setRemark(userBaseEntity.getRemark());

						}
					});
					return R.data(userVOIPage);
		}
		return R.data(resultList);
	}



	/**
	 * 班级统计
	 */
	@GetMapping("/bigdata/classes-statistics")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "数据大屏-班级统计", notes = "")
	public R bigdataClassesStatistics( ) {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		Map<String,Object> reuslt=new HashMap<>();
		Map<String, LocalDateTime> currentYearConvert = currentYearConvert(DateUtil.format(new Date(), "yyyy"));

		try {
			//1.班级 总数据  本年度数据
			long classesTotal=classesService.count();
			long classesYear=classesService.count(Wrappers.<ClassesEntity>query().lambda()
				.ge(ClassesEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME))
				.le(ClassesEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME)));


			//2.培训人数(有可能大于系统总人数，此处计算的是培训的人数次) 总数据  本年度数据
			long trainnerUserTotal=classesUserService.count();
			long trainnerUserYear=classesUserService.count(Wrappers.<ClassesUserEntity>query().lambda()
				.ge(ClassesUserEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME))
				.le(ClassesUserEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME)));

			//3.平均成绩 总数据  本年度数据 查询所有考试以及本年度的考试
			//3.1总数居
			QueryWrapper<PaperAnswerEntity> answerTotalWrapper = new QueryWrapper<>();
			answerTotalWrapper.select("sum(score) as score");
			answerTotalWrapper.lambda().eq(PaperAnswerEntity::getExamineType,2);
			Map<String, Object> totalMap = answerService.listMaps(answerTotalWrapper).get(0);
			//3.1.1成绩总和
			Double answerTotalScoreSum = (Double) totalMap.get("score");
			//3.1.2总答总人数
			long answerTotalCount = answerService.count(Wrappers.<PaperAnswerEntity>query().lambda().eq(PaperAnswerEntity::getExamineType, 2));

			//3.1.3总的平均成绩
			String totalAverage=df.format(answerTotalScoreSum/answerTotalCount);

			//3.2年度数据
			QueryWrapper<PaperAnswerEntity> answerYearWrapper = new QueryWrapper<>();
			answerYearWrapper.select("sum(score) as score");
			answerYearWrapper.lambda().eq(PaperAnswerEntity::getExamineType,2);
			answerYearWrapper.lambda().ge(PaperAnswerEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME));
			answerYearWrapper.lambda().le(PaperAnswerEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME));
			Map<String, Object> yearMap = answerService.listMaps(answerYearWrapper).get(0);
			//3.2.1成绩总和
			Double answerYearScoreSum = (Double) yearMap.get("score");
			//3.2.2总答总人数
			long answerYearCount = answerService.count(Wrappers.<PaperAnswerEntity>query().lambda().eq(PaperAnswerEntity::getExamineType, 2));

			//3.2.3总的平均成绩
			String yearAverage=df.format(answerYearScoreSum/answerYearCount);

			//总作答人数
			long totalAnswerUserCount=answerService.count();
			//年度作答人数
			long yearAnswerUserCount=answerService.count(Wrappers.<PaperAnswerEntity>query()
				.select("DISTINCT user_id,paper_release_id")
				.lambda()
				.ge(PaperAnswerEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME))
				.le(PaperAnswerEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME)).eq(PaperAnswerEntity::getExamineType,2)
			);

			//4.达标率 总数据  本年度数据  成绩超过总分的60%算合格
			//总数据-达标率
			Long passTotalCountSelect = answerService.passCount(null, null);
			long passTotalCount =passTotalCountSelect==null?0:passTotalCountSelect;
			String totalAnswerPass=df.format(passTotalCount/totalAnswerUserCount*100)+"%";

			//年度数据-达标率
			Long passYearCountSelect = answerService.passCount(currentYearConvert.get(YEAR_START_TIME), currentYearConvert.get(YEAR_END_TIME));
			long passYearCount = passYearCountSelect==null?0:passYearCountSelect;
			String yearAnswerPass=df.format(passYearCount/yearAnswerUserCount*100)+"%";

			//优秀率 总数据  本年度数据  成绩超过总分的80%算优秀
			//总数据-优秀率
			Long excellentCountSelect= answerService.excellentCount(null, null);
			long excellentTotalCount=excellentCountSelect==null?0:excellentCountSelect;
			String totalAnswerExcellent=df.format(excellentTotalCount/totalAnswerUserCount*100)+"%";

			//年度数据-优秀率
			Long excellentYearCountSelect = answerService.excellentCount(currentYearConvert.get(YEAR_START_TIME), currentYearConvert.get(YEAR_END_TIME));
			long excellentYearCount = excellentYearCountSelect==null?0:excellentYearCountSelect;
			String yearAnswerExcellent=df.format(excellentYearCount/yearAnswerUserCount*100)+"%";

			reuslt.put("classesTotal",classesTotal);//班级-总数据
			reuslt.put("classesYear",classesYear);//班级-年度数据
			reuslt.put("trainnerUserTotal",trainnerUserTotal);//人数-总数据
			reuslt.put("trainnerUserYear",trainnerUserYear);//人数-年度数据
			reuslt.put("totalAverage",totalAverage);//平均成绩-总数据
			reuslt.put("yearAverage",yearAverage);//平均成绩-年度数据
			reuslt.put("totalAnswerPass",totalAnswerPass);//达标率-总数据
			reuslt.put("yearAnswerPass",yearAnswerPass);//达标率-年度数据
			reuslt.put("totalAnswerExcellent",totalAnswerExcellent);//优秀率-总数据
			reuslt.put("yearAnswerExcellent",yearAnswerExcellent);//优秀率-年度数据
		}catch (Exception e){
			log.info(e.getMessage());
		}

		return R.data(reuslt);
	}



	/**
	 * 班级统计-明细列表
	 * 各类的列表
	 */
	@GetMapping("/bigdata/classes-statistics-list")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "数据大屏-班级统计-明细列表", notes = "班级列表-1，班级数据-2，人数-3")
	public R bigdataSystemClassesStatisticsList(Integer type) {
		List<Object> resultList=new ArrayList<>();
		String year= DateUtil.format(new Date(), "yyyy");
		Integer[] monthList = getMonth();
		Query query =new Query();
		query.setSize(500);//设置最大查询500条
		switch (type){
			case 1: //班级列表
				LambdaQueryWrapper<ClassesEntity> classesEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
				IPage<ClassesEntity> classesPages = classesService.page(Condition.getPage(query),classesEntityLambdaQueryWrapper);
				IPage<ClassesVO> classesVOIPage = ClassesWrapper.build().pageVO(classesPages);
				classesVOIPage.getRecords().forEach(vopage->{
					vopage.setStudents(classesUserService.count(Wrappers.<ClassesUserEntity>query().lambda().eq(ClassesUserEntity::getClassesId, vopage.getId())));
					List<ClassesPlanEntity> classesPlanEntityList = classesPlanService.list(Wrappers.<ClassesPlanEntity>query().lambda()
						.eq(ClassesPlanEntity::getClassesId, vopage.getId()));
					if (CollectionUtil.isNotEmpty(classesPlanEntityList)) {
						ClassesPlanEntity classesPlanEntity=classesPlanEntityList.get(0);
						TrainingPlanEntity trainingPlanEntity = trainingPlanService.getById(classesPlanEntity.getPlanId());
						if (null != trainingPlanEntity) {
							vopage.setTrainingPlanName(trainingPlanEntity.getPlanName());
							vopage.setClassesPlan(ClassesPlanWrapper.build().entityVO(classesPlanEntity));
							vopage.setTrainPlanId(trainingPlanEntity.getId());
							vopage.setIsExam(trainingPlanEntity.getIsExam());//是否需要考核(0 否，1是),如果是1，需要安排考试
						}
					}
				});
				return R.data(classesVOIPage);
			case 2: //班级数据 当年度的每月数据
				List<Long> classesCountList=new ArrayList<>();
				for (Integer month : monthList) {
					LocalDateTime startTime = LocalDateTime.of(Integer.parseInt(year), month, 1, 0, 0, 0);
					LocalDateTime endTime = startTime.with(TemporalAdjusters.lastDayOfMonth());//当月最后一天
					long count=classesService.count(Wrappers.<ClassesEntity>query().lambda()
						.ge(ClassesEntity::getCreateTime,startTime)
						.le(ClassesEntity::getCreateTime,endTime));
					classesCountList.add(count);
				}
				return R.data(classesCountList);
			case 3: //人数(每月管理的班级人数) 当年度的每月数据
				List<Long> classesUserCountList=new ArrayList<>();
				for (Integer month : monthList) {
					LocalDateTime startTime = LocalDateTime.of(Integer.parseInt(year), month, 1, 0, 0, 0);
					LocalDateTime endTime = startTime.with(TemporalAdjusters.lastDayOfMonth());//当月最后一天
					long count=classesUserService.count(Wrappers.<ClassesUserEntity>query().lambda()
						.ge(ClassesUserEntity::getCreateTime,startTime)
						.le(ClassesUserEntity::getCreateTime,endTime));
					classesUserCountList.add(count);
				}

				return R.data(classesUserCountList);

		}
		return R.data(resultList);
	}


	/**
	 * 培训教师数据
	 */
	@GetMapping("/bigdata/teacher-statistics")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "数据大屏-培训教师统计", notes = "")
	public R bigdataTeacherStatistics( ) {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		Map<String,Object> reuslt=new HashMap<>();
		Map<String, LocalDateTime> currentYearConvert = currentYearConvert(DateUtil.format(new Date(), "yyyy"));

		//新增教师数 总/年度
		long totalTeacherCount = userTeacherService.count();
		long yearTeacherCount = userTeacherService.count();

		//解聘教师数 总/年度
		long totalDeleteTeacherCount = userTeacherService.deleteTeacherCount(null,null);
		long yearDeleteTeacherCount = userTeacherService.deleteTeacherCount(currentYearConvert.get(YEAR_START_TIME),currentYearConvert.get(YEAR_END_TIME));

		//发布视频数 总/年度
		long totalVedioCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
			.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_VEDIO.getCategory()));
		long yearVedioCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
			.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_VEDIO.getCategory())
			.ge(CourseChapterSubFileEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME))
			.le(CourseChapterSubFileEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME)));

		//发布课件数 总/年度
		long totalCoursewareCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
			.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_COURSEWARE.getCategory()));
		long yearCoursewareCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
			.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_COURSEWARE.getCategory())
			.ge(CourseChapterSubFileEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME))
			.le(CourseChapterSubFileEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME)));

		//发布题库数 总/年度
		long totalQuestionBankCount = questionBankService.count();
		long yearQuestionBankCount = questionBankService.count(Wrappers.<QuestionBankEntity>query().lambda()
			.ge(QuestionBankEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME))
			.le(QuestionBankEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME)));


		reuslt.put("totalTeacherCount",totalTeacherCount);//新增教师数-总数据
		reuslt.put("yearTeacherCount",yearTeacherCount);//新增教师数-年度数据
		reuslt.put("totalDeleteTeacherCount",totalDeleteTeacherCount);//解聘教师数-总数据
		reuslt.put("yearDeleteTeacherCount",yearDeleteTeacherCount);//解聘教师数-年度数据
		reuslt.put("totalVedioCount",totalVedioCount);//发布视频数-总数据
		reuslt.put("yearVedioCount",yearVedioCount);//发布视频数-年度数据
		reuslt.put("totalCoursewareCount",totalCoursewareCount);//发布课件数-总数据
		reuslt.put("yearCoursewareCount",yearCoursewareCount);//发布课件数-年度数据
		reuslt.put("totalQuestionBankCount",totalQuestionBankCount);//发布题库数-总数据
		reuslt.put("yearQuestionBankCount",yearQuestionBankCount);//发布题库数-年度数据
		return R.data(reuslt);
	}


	@GetMapping("/bigdata/teacher-statistics-detail")
	@ApiOperationSupport(order = 21)
	@ApiOperation(value = "数据大屏-培训教师统计-当年度月份数据", notes = "")
	public R bigdataTeacherStatisticsDetail( ) {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		Map<String,Object> reuslt=new HashMap<>();
		Map<String, LocalDateTime> currentYearConvert = currentYearConvert(DateUtil.format(new Date(), "yyyy"));
		Integer[] monthList = getMonth();
		String year=DateUtil.format(new Date(),"yyyy");
		//新增教师数
		List<Long> increaseTeacherListMonth=new ArrayList<>();
		//解聘教师数
		List<Long> deleteTeacherListMonth=new ArrayList<>();
		//发布视频数
		List<Long> vedioListMonth=new ArrayList<>();
		//发布课件数
		List<Long> coursewareListMonth=new ArrayList<>();
		//发布题库数
		List<Long> questionBankListMonth=new ArrayList<>();

		for (Integer month:monthList){
			LocalDateTime startTime=LocalDateTime.of(Integer.parseInt(year),month,1,0,0,0);
			LocalDateTime endTime = startTime.with(TemporalAdjusters.lastDayOfMonth());//当月最后一天

			//教师数
			long monthTeacherCount = userTeacherService.count(Wrappers.<UserTeacherEntity>query().lambda()
				.ge(UserTeacherEntity::getCreateTime,startTime)
				.le(UserTeacherEntity::getCreateTime,endTime));
			//解聘教师数
			long monthDeleteTeacherCount = userTeacherService.deleteTeacherCount(startTime,endTime);

			//发布视频数
			long monthVedioCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
				.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_VEDIO.getCategory())
				.ge(CourseChapterSubFileEntity::getCreateTime,startTime)
				.le(CourseChapterSubFileEntity::getCreateTime,endTime));

			//发布课件数
			long monthCoursewareCount = courseChapterSubFileService.count(Wrappers.<CourseChapterSubFileEntity>query().lambda()
				.eq(CourseChapterSubFileEntity::getAttachType, AttachType.COURSE_COURSEWARE.getCategory())
				.ge(CourseChapterSubFileEntity::getCreateTime,startTime)
				.le(CourseChapterSubFileEntity::getCreateTime,endTime));

			//发布题库数
			long monthQuestionBankCount = questionBankService.count(Wrappers.<QuestionBankEntity>query().lambda()
				.ge(QuestionBankEntity::getCreateTime,startTime)
				.le(QuestionBankEntity::getCreateTime,endTime));

			increaseTeacherListMonth.add(monthTeacherCount);
			deleteTeacherListMonth.add(monthDeleteTeacherCount);
			vedioListMonth.add(monthVedioCount);
			coursewareListMonth.add(monthCoursewareCount);
			questionBankListMonth.add(monthQuestionBankCount);
		}
		reuslt.put("increaseTeacherListMonth",increaseTeacherListMonth);//新增教师数-月份
		reuslt.put("deleteTeacherListMonth",deleteTeacherListMonth);//解聘教师数-月份
		reuslt.put("vedioListMonth",vedioListMonth);//发布视频数-月份
		reuslt.put("coursewareListMonth",coursewareListMonth);//发布课件数-月份
		reuslt.put("questionBankListMonth",questionBankListMonth);//发布题库数-月份
		return R.data(reuslt);
	}


	/**
	 * 教师统计
	 * 各类的列表
	 */
	@GetMapping("/bigdata/teacher-statistics-list")
	@ApiOperationSupport(order = 22)
	@ApiOperation(value = "数据大屏-教师-各类列表", notes = "新增列表-1，解聘列表-2")
	public R bigdataTeacherList(Integer type) {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		List<Object> resultList=new ArrayList<>();
		String year= DateUtil.format(new Date(), "yyyy");
		Integer[] monthList = getMonth();
		Query query =new Query();
		query.setSize(500);//设置最大查询500条
		switch (type){
			case 1: //新增列表
				List<UserTeacherEntity> userTeacherList = userTeacherService.list();
				if (CollectionUtil.isEmpty(userTeacherList)) return R.success("暂无数据");
				List<Long> teacherIds = userTeacherList.stream().map(UserTeacherEntity::getUserId).collect(Collectors.toList());

				LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
				userLambdaQueryWrapper.in(User::getId,teacherIds);
				IPage<User> userPages = userService.page(Condition.getPage(query), userLambdaQueryWrapper);
				IPage<UserVO> userVOIPage = UserWrapper.build().pageVO(userPages);
				userVOIPage.getRecords().forEach(userVo -> {
					UserBaseEntity userBaseEntity = userBaseService.selectByUserId(userVo.getId());
					if (null != userBaseEntity) {
						userVo.setIdCard(userBaseEntity.getUserNumber());
						userVo.setUserName(userBaseEntity.getUserName());
						userVo.setTrainingMajorCategory(userBaseEntity.getTrainingMajorCategory());
						userVo.setTrainingMajorDirection(userBaseEntity.getTrainingMajorDirection());
						userVo.setAppointmentGrade(userBaseEntity.getAppointmentGrade());
						userVo.setRemark(userBaseEntity.getRemark());

					}
				});
				return R.data(userVOIPage);

			case 2: //积分年度数据 当年度的每月数据
				List<UserTeacherEntity> deleteTeacherList = userTeacherService.deleteTeacherList(null,null);
				if (CollectionUtil.isEmpty(deleteTeacherList)) return R.success("暂无数据");
				List<Long> teacherDeleteIds = deleteTeacherList.stream().map(UserTeacherEntity::getUserId).collect(Collectors.toList());

				LambdaQueryWrapper<User> teacherDeleteLambdaQueryWrapper = new LambdaQueryWrapper<>();
				teacherDeleteLambdaQueryWrapper.in(User::getId,teacherDeleteIds);
				IPage<User> teacherDeletePages = userService.page(Condition.getPage(query), teacherDeleteLambdaQueryWrapper);
				IPage<UserVO> teacherDeleteVoPages = UserWrapper.build().pageVO(teacherDeletePages);
				teacherDeleteVoPages.getRecords().forEach(userVo -> {
					UserBaseEntity userBaseEntity = userBaseService.selectByUserId(userVo.getId());
					if (null != userBaseEntity) {
						userVo.setIdCard(userBaseEntity.getUserNumber());
						userVo.setUserName(userBaseEntity.getUserName());
						userVo.setTrainingMajorCategory(userBaseEntity.getTrainingMajorCategory());
						userVo.setTrainingMajorDirection(userBaseEntity.getTrainingMajorDirection());
						userVo.setAppointmentGrade(userBaseEntity.getAppointmentGrade());
						userVo.setRemark(userBaseEntity.getRemark());

					}
				});
				return R.data(teacherDeleteVoPages);
		}
		return R.data(resultList);
	}


	/**
	 * 积分、学时统计
	 */
	@GetMapping("/bigdata/integral-period-statistics")
	@ApiOperationSupport(order = 30)
	@ApiOperation(value = "数据大屏-积分、学时统计", notes = "")
	public R bigdataIntegralPeriodStatistics() {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		String year=DateUtil.format(new Date(),"yyyy");
		Map<String,Object> reuslt=new HashMap<>();
		Map<String, LocalDateTime> currentYearConvert = currentYearConvert(DateUtil.format(new Date(), "yyyy"));
		try {
			//积分 总/年度
			QueryWrapper<IntegralLogEntity> integralLogQueryWrapperTotal = new QueryWrapper<>();
			integralLogQueryWrapperTotal.select("sum(integral) as integral");
			Map<String, Object> integralLogTotalMap = integralLogService.listMaps(integralLogQueryWrapperTotal).get(0);
			Double integralTotalSum = (Double) integralLogTotalMap.get("integral");//总积分

			QueryWrapper<IntegralLogEntity> integralLogQueryWrapperYear = new QueryWrapper<>();
			integralLogQueryWrapperYear.select("sum(integral) as integral");
			integralLogQueryWrapperYear.lambda().eq(IntegralLogEntity::getYear,year);
			Map<String, Object> integralLogYearMap = integralLogService.listMaps(integralLogQueryWrapperYear).get(0);
			Double integralYearSum = (Double) integralLogYearMap.get("integral");//年度积分


			//获得过积分的人
			List<Long> totalUserIds = integralLogService.list().stream().map(IntegralLogEntity::getUserId).distinct().collect(Collectors.toList());

			//当年度获得过积分的人
			List<Long> yearUserIds = integralLogService.list(Wrappers.<IntegralLogEntity>query().lambda().eq(IntegralLogEntity::getYear,year))
				.stream().map(IntegralLogEntity::getUserId).distinct().collect(Collectors.toList());

			//积分达标率 总/年度
			QueryWrapper<IntegralYearLogEntity> integralPassTotalQueryMrapper=new QueryWrapper<>();
			integralPassTotalQueryMrapper.apply("integral >= integral_target");
			long integralPassCount = integralYearLogService.count(integralPassTotalQueryMrapper);
			long integralCount = integralYearLogService.count();
			String integralPassTotal=df.format(integralPassCount/integralCount*100)+"%";//总


			QueryWrapper<IntegralYearLogEntity> integralPassYearQueryMrapper=new QueryWrapper<>();
			integralPassYearQueryMrapper.apply("integral >= integral_target");
			integralPassYearQueryMrapper.lambda().eq(IntegralYearLogEntity::getYear,year);
			long integralPassCountYear = integralYearLogService.count(integralPassYearQueryMrapper);
			long integralCountYear = integralYearLogService.count(Wrappers.<IntegralYearLogEntity>query().lambda().eq(IntegralYearLogEntity::getYear,year));
			String integralPassYear=df.format(integralPassCountYear/integralCountYear*100)+"%";//年度

			//人均积分 总/年度
			String averageIntegralTotal=df.format(integralTotalSum/totalUserIds.size());
			String averageIntegralYear=df.format(integralYearSum/yearUserIds.size());

			//学时 总/年度
			QueryWrapper<IntegralLogEntity> periodQueryWrapperTotal = new QueryWrapper<>();
			periodQueryWrapperTotal.select("sum(period) as period");
			Map<String, Object> periodTotalMap = integralLogService.listMaps(periodQueryWrapperTotal).get(0);
			int periodTotalSum =Integer.parseInt( ((BigDecimal) periodTotalMap.get("period")).toString());//总积分

			QueryWrapper<IntegralLogEntity> periodQueryWrapperYear = new QueryWrapper<>();
			periodQueryWrapperYear.select("sum(period) as period");
			periodQueryWrapperYear.lambda().eq(IntegralLogEntity::getYear,year);
			Map<String, Object> periodYearMap = integralLogService.listMaps(periodQueryWrapperYear).get(0);
			int periodYearSum =Integer.parseInt( ((BigDecimal) periodYearMap.get("period")).toString());//年度积分

			//人均学时 总/年度
			String averagePeriodTotal=df.format(periodTotalSum/totalUserIds.size());
			String averagePeriodYear=df.format(periodYearSum/yearUserIds.size());

			reuslt.put("integralTotalSum",integralTotalSum);//积分-总数据
			reuslt.put("integralYearSum",integralYearSum);//积分-年度数据

			reuslt.put("integralPassTotal",integralPassTotal);//积分达标率-总数据
			reuslt.put("integralPassYear",integralPassYear);//积分达标率-年度数据

			reuslt.put("averageIntegralTotal",averageIntegralTotal);//人均积分-总数据
			reuslt.put("averageIntegralYear",averageIntegralYear);//人均积分-年度数据

			reuslt.put("periodTotalSum",periodTotalSum);//学时-总数据
			reuslt.put("periodYearSum",periodYearSum);//学时-年度数据

			reuslt.put("averagePeriodTotal",averagePeriodTotal);//人均学时-总数据
			reuslt.put("averagePeriodYear",averagePeriodYear);//人均学时-年度数据

		}catch (Exception e){
			log.info(e.getMessage());
		}
		return R.data(reuslt);
	}


	/**
	 * 积分、学时统计
	 * 各类的列表
	 */
	@GetMapping("/bigdata/integral-period-statistics-list")
	@ApiOperationSupport(order = 31)
	@ApiOperation(value = "数据大屏-积分、学时统计-明细列表", notes = "积分列表-1，积分年度数据-2，学时列表-3，学时年度数据-4,积分达标率年度数据-5")
	public R bigdataIntegralPeriodList(Integer type) {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		List<Object> resultList=new ArrayList<>();
		String year= DateUtil.format(new Date(), "yyyy");
		Integer[] monthList = getMonth();
		Query query =new Query();
		query.setSize(500);//设置最大查询500条
		try {
			switch (type){
				case 1: //积分列表
					LambdaQueryWrapper<IntegralLogEntity> integralLogEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
					integralLogEntityLambdaQueryWrapper.orderByDesc(IntegralLogEntity::getCreateTime);
					integralLogEntityLambdaQueryWrapper.gt(IntegralLogEntity::getIntegral,0);
					IPage<IntegralLogEntity> integralPages = integralLogService.page(Condition.getPage(query),integralLogEntityLambdaQueryWrapper);
					return R.data(integralPages);
				case 2: //积分年度数据 当年度的每月数据
					List<Double> integralCountList=new ArrayList<>();
					for (Integer month : monthList) {
						Double integralTotalSum= Double.valueOf(0);
						QueryWrapper<IntegralLogEntity> monthQueryMrapper=new QueryWrapper<>();
						monthQueryMrapper.select("sum(integral) as integral");
						LocalDateTime startTime = LocalDateTime.of(Integer.parseInt(year), month, 1, 0, 0, 0);
						LocalDateTime endTime = startTime.with(TemporalAdjusters.lastDayOfMonth());//当月最后一天
						monthQueryMrapper.lambda().ge(IntegralLogEntity::getCreateTime,startTime);
						monthQueryMrapper.lambda().le(IntegralLogEntity::getCreateTime,endTime);
						Map<String, Object> integralLogTotalMap = integralLogService.listMaps(monthQueryMrapper).get(0);
						if (null!=integralLogTotalMap)integralTotalSum = (Double) integralLogTotalMap.get("integral");//总积分
						integralCountList.add(integralTotalSum);
					}

					return R.data(integralCountList);
				case 3: //学时列表
					LambdaQueryWrapper<IntegralLogEntity> periodLambdaQueryWrapper=new LambdaQueryWrapper<>();
					periodLambdaQueryWrapper.orderByDesc(IntegralLogEntity::getCreateTime);
					periodLambdaQueryWrapper.gt(IntegralLogEntity::getPeriod,0);
					IPage<IntegralLogEntity> periodPages = integralLogService.page(Condition.getPage(query),periodLambdaQueryWrapper);
					return R.data(periodPages);

				case 4: //学时年度数据
					List<Integer> periodCountList=new ArrayList<>();
					for (Integer month : monthList) {
						int periodSum=0;
						QueryWrapper<IntegralLogEntity> monthQueryMrapper=new QueryWrapper<>();
						monthQueryMrapper.select("sum(period) as period");
						LocalDateTime startTime = LocalDateTime.of(Integer.parseInt(year), month, 1, 0, 0, 0);
						LocalDateTime endTime = startTime.with(TemporalAdjusters.lastDayOfMonth());//当月最后一天
						monthQueryMrapper.lambda().ge(IntegralLogEntity::getCreateTime,startTime);
						monthQueryMrapper.lambda().le(IntegralLogEntity::getCreateTime,endTime);
						Map<String, Object> integralLogTotalMap = integralLogService.listMaps(monthQueryMrapper).get(0);
						if(null!=integralLogTotalMap)periodSum =Integer.parseInt( ((BigDecimal) integralLogTotalMap.get("period")).toString());//总积分
						periodCountList.add(periodSum);
					}
					return R.data(periodCountList);
				case 5: //积分达标率年度数据
					List<String> passRateList=new ArrayList<>();
					for (Integer month : monthList) {
						QueryWrapper<IntegralYearLogEntity> monthQueryMrapper=new QueryWrapper<>();
						LocalDateTime startTime = LocalDateTime.of(Integer.parseInt(year), month, 1, 0, 0, 0);
						LocalDateTime endTime = startTime.with(TemporalAdjusters.lastDayOfMonth());//当月最后一天
						monthQueryMrapper.lambda().ge(IntegralYearLogEntity::getCreateTime,startTime);
						monthQueryMrapper.lambda().le(IntegralYearLogEntity::getCreateTime,endTime);
						monthQueryMrapper.apply("integral >= integral_target");
						long integralPassCount = integralYearLogService.count(monthQueryMrapper);
						long integralCount = integralYearLogService.count();
						String passRate=df.format(integralPassCount/integralCount*100)+"%";
						passRateList.add(passRate);
					}
					return R.data(passRateList);
			}
		}catch (Exception e){
			log.info(e.getMessage());
		}

		return R.data(resultList);
	}



	/**
	 * 培训统计
	 */
	@GetMapping("/bigdata/training-statistics")
	@ApiOperationSupport(order = 40)
	@ApiOperation(value = "数据大屏-培训", notes = "")
	public R bigdataStatisticsTraining( ) {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		String year=DateUtil.format(new Date(),"yyyy");
		Map<String,Object> reuslt=new HashMap<>();
		Map<String, LocalDateTime> currentYearConvert = currentYearConvert(DateUtil.format(new Date(), "yyyy"));
		try {
			//计划培训 总/年度
			Set<String> yearKeys = bladeRedis.keys(RedisKeyConstant.TRAINING_PLAN_YEAR + "*");
			long yearTrainingCount=0;//年度
			long totalTrainingCount=0;//总
			for (String yearKey:yearKeys) {
				List<TrainingYearExcel> list = bladeRedis.get(yearKey);
				if (null!=list) totalTrainingCount+=list.size();
			}

			List<TrainingYearExcel> yearTrainingList = bladeRedis.get(RedisKeyConstant.TRAINING_PLAN_YEAR + year);
			if (null!=yearTrainingList)yearTrainingCount=yearTrainingList.size();

			//完成培训(已开展) 总/年度
			long totalTrainCompleteCount = trainingPlanService.count();
			long yearTrainCompleteCount = trainingPlanService.count(Wrappers.<TrainingPlanEntity>query().lambda().eq(TrainingPlanEntity::getYear,year));

			//培训人数 总/年度
			long totalTrainnerCount = classesUserService.count();
			long yearTrainnerCount = classesUserService.count(Wrappers.<ClassesUserEntity>query().lambda()
				.ge(ClassesUserEntity::getCreateTime,currentYearConvert.get(YEAR_START_TIME))
				.le(ClassesUserEntity::getCreateTime,currentYearConvert.get(YEAR_END_TIME)));

			//完成率 总/年度
			String totalCompleteRate=df.format(totalTrainCompleteCount/totalTrainingCount*100)+"%";
			String yearCompleteRate=df.format(yearTrainingCount/yearTrainCompleteCount*100)+"%";

			reuslt.put("totalTrainingCount",totalTrainingCount);//计划培训-总
			reuslt.put("yearTrainingCount",yearTrainingCount);//计划培训-年度

			reuslt.put("totalTrainCompleteCount",totalTrainCompleteCount);//完成培训-总
			reuslt.put("yearTrainCompleteCount",yearTrainCompleteCount);//完成培训-年度

			reuslt.put("totalTrainnerCount",totalTrainnerCount);//培训人数-总
			reuslt.put("yearTrainnerCount",yearTrainnerCount);//培训人数-年度

			reuslt.put("totalCompleteRate",totalCompleteRate);//完成率-总
			reuslt.put("yearCompleteRate",yearCompleteRate);//完成率-年度
		}catch (Exception e){
			log.info(e.getMessage());
		}

		return R.data(reuslt);
	}


	/**
	 * 考核统计
	 */
	@GetMapping("/bigdata/examination-statistics")
	@ApiOperationSupport(order = 41)
	@ApiOperation(value = "数据大屏-年度考核", notes = "")
	public R bigdataStatisticsExamination( ) {
		DecimalFormat df=new DecimalFormat("0.##");//保留有意义的小数点后两位 例如0.1
		String year=DateUtil.format(new Date(),"yyyy");
		Map<String,Object> reuslt=new HashMap<>();
		Map<String, LocalDateTime> currentYearConvert = currentYearConvert(DateUtil.format(new Date(), "yyyy"));

		try {
			//考核实施 (发布的考试)
			long yearReleaseCount = paperReleaseService.count(Wrappers.<PaperReleaseEntity>query().lambda()
				.ge(PaperReleaseEntity::getCreateTime, currentYearConvert.get(YEAR_START_TIME))
				.le(PaperReleaseEntity::getCreateTime, currentYearConvert.get(YEAR_END_TIME)));

			//考核人数
			long yearReleaseUserCount = releaseExamUserService.count(Wrappers.<ReleaseExamUserEntity>query().lambda()
				.ge(ReleaseExamUserEntity::getCreateTime, currentYearConvert.get(YEAR_START_TIME))
				.le(ReleaseExamUserEntity::getCreateTime, currentYearConvert.get(YEAR_END_TIME)));

			//参与人数
			long yearAnswerCount = answerService.count(Wrappers.<PaperAnswerEntity>query()
				.select("DISTINCT user_id,paper_release_id")
				.lambda()
				.ge(PaperAnswerEntity::getCreateTime, currentYearConvert.get(YEAR_START_TIME))
				.le(PaperAnswerEntity::getCreateTime, currentYearConvert.get(YEAR_END_TIME))
				.eq(PaperAnswerEntity::getExamineType, 2));
			//考核参与率
			String yearExamParticipateRate=df.format(yearAnswerCount/yearReleaseUserCount*100)+"%";

			//考核达标率
			Long passYearCountSelect = answerService.passCount(currentYearConvert.get(YEAR_START_TIME), currentYearConvert.get(YEAR_END_TIME));
			long passYearCount = passYearCountSelect==null?0:passYearCountSelect;
			String yearExamPass=df.format(passYearCount/yearReleaseUserCount*100)+"%";

			reuslt.put("yearReleaseCount",yearReleaseCount);//考核实施
			reuslt.put("yearReleaseUserCount",yearReleaseUserCount);//考核人数
			reuslt.put("yearExamParticipateRate",yearExamParticipateRate);//考核参与率
			reuslt.put("yearExamPass",yearExamPass);//考核达标率
		}catch (Exception e){
			log.info(e.getMessage());
		}

		return R.data(reuslt);
	}

	/**
	 * 培训台账
	 */
	@GetMapping("/bigdata/training-list")
	@ApiOperationSupport(order = 50)
	@ApiOperation(value = "数据大屏-培训台账", notes = "")
	public R bigdataTrainingList() {
		return R.data(trainingPlanService.list(Wrappers.<TrainingPlanEntity>query().lambda().orderByDesc(TrainingPlanEntity::getCreateTime)));
	}

	/**
	 * 积分台账
	 */
	@GetMapping("/bigdata/integral-list")
	@ApiOperationSupport(order = 51)
	@ApiOperation(value = "数据大屏-积分台账", notes = "")
	public R bigdataIntegralList() {
		return R.data(integralLogService.list(Wrappers.<IntegralLogEntity>query().lambda().orderByDesc(IntegralLogEntity::getCreateTime)));
	}


	/**
	 * 积分台账
	 */
	@GetMapping("/bigdata/item-list")
	@ApiOperationSupport(order = 51)
	@ApiOperation(value = "数据大屏-教具台账", notes = "")
	public R bigdataItemList() {
		LambdaQueryWrapper<ItemStockLogEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		List<ItemStockLogEntity> list = stockLogService.list(lambdaQueryWrapper);
		List<ItemStockLogVO> resultList = list.stream().map(entity -> {
			ItemStockLogVO itemStockLogVO = StockLogWrapper.build().entityVO(entity);
			UserBaseEntity userBaseEntity = userBaseService.selectByUserId(itemStockLogVO.getCreateUser());
			if (null != userBaseEntity){
				itemStockLogVO.setUserName(userBaseEntity.getUserName());
				itemStockLogVO.setIdCard(userBaseEntity.getUserNumber());
			}
			ItemEntity item = iItemService.getById(itemStockLogVO.getItemId());
			if (null != item) itemStockLogVO.setItemName(item.getItemName());

			return itemStockLogVO;
		}).collect(Collectors.toList());


		return R.data(resultList);
	}

	public static void year(){
		String year= DateUtil.format(new Date(), "yyyy");
		LocalDateTime startTime=LocalDateTime.of(Integer.parseInt(year),1,1,0,0,0);
		LocalDateTime endTime=LocalDateTime.of(Integer.parseInt(year),12,31,23,59,59);
	}

	public static Integer[] getMonth(){
		return new Integer[]{1,2,3,4,5,6,7,8,9,10,11,12};
	}






	@GetMapping("/teacher-statistics-list")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "老师统计数据分页列表", notes = "老师统计数据分页列表")
	public R teacherStatisticsList(Query query, String realName) {
		List<UserTeacherEntity> teacherList = userTeacherService.list();
		if (CollectionUtil.isEmpty(teacherList)) return R.success("暂无数据");

		LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		lambdaQueryWrapper.in(User::getId,teacherList.stream().map(UserTeacherEntity::getUserId).collect(Collectors.toList()));
		if (StringUtils.isNotBlank(realName)) {//如果有搜索 则默认搜索所有老师和名字匹配
			lambdaQueryWrapper.like(User::getRealName,realName);
		}
		IPage<User> pages = userService.page(Condition.getPage(query), lambdaQueryWrapper);
		IPage<UserVO> userVOIPage = UserWrapper.build().pageVO(pages);
		userVOIPage.getRecords().forEach(uservo -> {
			//1.课程作业数
			long courseHomewordNum = paperReleaseService.count(Wrappers.<PaperReleaseEntity>query().lambda().eq(PaperReleaseEntity::getCreateUser, uservo.getId()));

			//2.线下实训数
			long trainArchiveNum = trainArchiveService.count(Wrappers.<TrainArchiveEntity>query().lambda().eq(TrainArchiveEntity::getTeacherName, uservo.getRealName()));

			//3.发布资源数
			long releaseMaterialNum = materialLibraryService.count(Wrappers.<MaterialLibraryEntity>query().lambda().eq(MaterialLibraryEntity::getCreateUser, uservo.getId()));

			//4.任教课程数
			long teachdNum = courseService.count(Wrappers.<CourseEntity>query().lambda().eq(CourseEntity::getTeacherId, uservo.getId()));

			uservo.setCourseHomewordNum(courseHomewordNum);
			uservo.setTrainArchiveNum(trainArchiveNum);
			uservo.setReleaseMaterialNum(releaseMaterialNum);
			uservo.setTeachdNum(teachdNum);
		});

		return R.data(userVOIPage);
	}

	@GetMapping("/trainee-statistics-list")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "学员统计数据分页列表", notes = "学员统计数据分页列表")
	public R traineeStatisticsList(Query query, String realName) {
		LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();

		if (StringUtils.isNotBlank(realName)) {//如果有搜索
			lambdaQueryWrapper.like(User::getRealName, realName);
		}

		IPage<User> pages = userService.page(Condition.getPage(query), lambdaQueryWrapper);
		IPage<UserVO> userVOIPage = UserWrapper.build().pageVO(pages);
		userVOIPage.getRecords().forEach(uservo -> {
			//1.受培训数
			long trainingNum = classesUserService.count(Wrappers.<ClassesUserEntity>query().lambda().eq(ClassesUserEntity::getUserId, uservo.getId()));

			//2.观看视频数
			long watchVedioNum = subVideoReadService.count(Wrappers.<SubVideoReadEntity>query().lambda().eq(SubVideoReadEntity::getUserId, uservo.getRealName()));

			//3.完成习题数
			long exercisesNum = courseChapterSubBankService.count(Wrappers.<CourseChapterSubBankEntity>query().lambda().eq(CourseChapterSubBankEntity::getCreateUser, uservo.getId()));

			//4.完成课程作业数
			long completeHomewordNum = answerService.count(Wrappers.<PaperAnswerEntity>query().lambda()
				.eq(PaperAnswerEntity::getUserId, uservo.getId())
				.eq(PaperAnswerEntity::getExamineType,1));

			uservo.setTrainingNum(trainingNum);
			uservo.setWatchVedioNum(watchVedioNum);
			uservo.setExercisesNum(exercisesNum);
			uservo.setCompleteHomewordNum(completeHomewordNum);

		});

		return R.data(userVOIPage);
	}

	@GetMapping("/item-statistics-list")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "教具统计数据分页列表", notes = "教具统计数据分页列表")
	public R itemStatisticsList(ItemVO item, Query query) {
		IPage<ItemVO> pages = iItemService.selectItemPage(Condition.getPage(query), item);
		List<ItemVO> records = pages.getRecords();
		records.forEach(record -> {
			record.setWareHouseNum(stockLogService.count(Wrappers.<ItemStockLogEntity>query().lambda().eq(ItemStockLogEntity::getType, ItemOperateType.WAREHOUSE.getCode()).eq(ItemStockLogEntity::getItemId,record.getId())));
			record.setOutStockNum(stockLogService.count(Wrappers.<ItemStockLogEntity>query().lambda().eq(ItemStockLogEntity::getType,ItemOperateType.OUTSTOCK.getCode()).eq(ItemStockLogEntity::getItemId,record.getId())));
			record.setBorrowNum(stockLogService.count(Wrappers.<ItemStockLogEntity>query().lambda().eq(ItemStockLogEntity::getType,ItemOperateType.BORROW.getCode()).eq(ItemStockLogEntity::getItemId,record.getId())));
			record.setDestoryNum(stockLogService.count(Wrappers.<ItemStockLogEntity>query().lambda().eq(ItemStockLogEntity::getType,ItemOperateType.DESTORY.getCode()).eq(ItemStockLogEntity::getItemId,record.getId())));
		});
		return R.data(pages);
	}

}
